package com.letv.core.db;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.letv.core.bean.PushBookLive;
import com.letv.core.constant.DatabaseConstant;
import com.letv.core.constant.LetvConstant;
import com.letv.core.utils.LetvTools;
import com.letv.core.utils.LogInfo;
import com.letv.core.utils.MD5;

public class LiveBookTraceHandler {

	private Context context;
	
	public LiveBookTraceHandler(Context context) {
		this.context = context;
	}

	/**
	 * 保存预定节目
	 * */
	public void saveLiveBookTrace(String programName , String channelName , String code , String play_time , long play_time_long, String id, int launchMode){
		
		String md5_id = MD5.toMd5(programName + code + play_time);
		
		if(!hasLiveBookTrace(md5_id)){
			ContentValues contentValues = new ContentValues();
			contentValues.put(DatabaseConstant.LiveBookTrace.Field.CHANNELNAME , channelName);
			contentValues.put(DatabaseConstant.LiveBookTrace.Field.PROGRAMNAME , programName);
			contentValues.put(DatabaseConstant.LiveBookTrace.Field.CODE , code);
			contentValues.put(DatabaseConstant.LiveBookTrace.Field.PLAY_TIME , play_time);
			contentValues.put(DatabaseConstant.LiveBookTrace.Field.PLAY_TIME_LONG , play_time_long);
			contentValues.put(DatabaseConstant.LiveBookTrace.Field.MD5_ID , md5_id);
			contentValues.put(DatabaseConstant.LiveBookTrace.Field.IS_NOTIFY , 0);
			contentValues.put(DatabaseConstant.LiveBookTrace.Field.LIVE_ID , id);
			contentValues.put(DatabaseConstant.LiveBookTrace.Field.LAUNCH_MODE , launchMode);
			
			context.getContentResolver().insert(LetvContentProvider.URI_LIVEBOOKTRACE, contentValues);
			LogInfo.log("zlb", "数据库添加成功：" + channelName + " , " + programName + " , launchMode = " + launchMode);
			LetvTools.logBook("数据库添加成功：" + channelName + " , " + programName + " , launchMode = " + launchMode, getClass());
		} else {
			LogInfo.log("zlb", "数据库已有：" + channelName + " , " + programName + " , 不添加");
			LetvTools.logBook("数据库已有：" + channelName + " , " + programName + " , 不添加", getClass());
		}
	}
	
	/**
	 * 得到最近的预定的接口的时间，没有预定返回-1
	 * */
	public long getNearestTrace(){
		Cursor cursor = null ;
		try{
			cursor = context.getContentResolver().query(LetvContentProvider.URI_LIVEBOOKTRACE, null, DatabaseConstant.LiveBookTrace.Field.PLAY_TIME_LONG + ">? AND " + DatabaseConstant.LiveBookTrace.Field.IS_NOTIFY + "<>?",
					new String[] {(System.currentTimeMillis() - 10 * 60 * 1000) + "" , "1"} , DatabaseConstant.LiveBookTrace.Field.PLAY_TIME_LONG);
			if(cursor != null && cursor.getCount() > 0){
				cursor.moveToFirst();
				return cursor.getLong(cursor.getColumnIndexOrThrow(DatabaseConstant.LiveBookTrace.Field.PLAY_TIME_LONG)) ;
			}else{
				return -1 ;
			}
		}finally{
			LetvTools.closeCursor(cursor);
		}
	}
	
	/**
	 * 得到当前时间，前后10分钟内的节目
	 * */
	public ArrayList<PushBookLive> getCurrentTrace(){
		Cursor cursor = null ;
		ArrayList<PushBookLive> list = null ;
		long currentTime = System.currentTimeMillis();
		try{
			cursor = context.getContentResolver().query(LetvContentProvider.URI_LIVEBOOKTRACE, null, DatabaseConstant.LiveBookTrace.Field.PLAY_TIME_LONG + " > ? AND " + DatabaseConstant.LiveBookTrace.Field.PLAY_TIME_LONG + " < ? AND " + DatabaseConstant.LiveBookTrace.Field.IS_NOTIFY + "<>?", new String[]{(currentTime - 10 * 60 * 1000)+"" , (currentTime + 10 * 60 * 1000)+"" , "1"}, DatabaseConstant.LiveBookTrace.Field.PLAY_TIME_LONG);
			list = new ArrayList<PushBookLive>() ;
			
			while(cursor.moveToNext()){
				PushBookLive bookLive = new PushBookLive() ;
				
				bookLive.setChannelName(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.LiveBookTrace.Field.CHANNELNAME)));
				bookLive.setProgramName(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.LiveBookTrace.Field.PROGRAMNAME)));
				bookLive.setCode(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.LiveBookTrace.Field.CODE)));
				bookLive.setPlay_time(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.LiveBookTrace.Field.PLAY_TIME)));
				bookLive.setLaunchMode(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.LiveBookTrace.Field.LAUNCH_MODE)));
				bookLive.setId(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.LiveBookTrace.Field.LIVE_ID)));

				list.add(bookLive);
			}
		}finally{
			LetvTools.closeCursor(cursor);
		}
		
		return list ;
	}
	
	/**
	 * 得到所有节目记录
	 * */
	public ArrayList<PushBookLive> getAllTrace() {
		Cursor cursor = null ;
		ArrayList<PushBookLive> list = null ;
		try {
			cursor = context.getContentResolver().query(LetvContentProvider.URI_LIVEBOOKTRACE, null, null, null, null);
			list = new ArrayList<PushBookLive>() ;
			
			while(cursor.moveToNext()){
				PushBookLive bookLive = new PushBookLive() ;
				
				bookLive.setMd5_id(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.LiveBookTrace.Field.MD5_ID)));
				bookLive.setChannelName(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.LiveBookTrace.Field.CHANNELNAME)));
				bookLive.setProgramName(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.LiveBookTrace.Field.PROGRAMNAME)));
				bookLive.setCode(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.LiveBookTrace.Field.CODE)));
				bookLive.setPlay_time(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.LiveBookTrace.Field.PLAY_TIME)));
				bookLive.setLaunchMode(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.LiveBookTrace.Field.LAUNCH_MODE)));
				bookLive.setId(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.LiveBookTrace.Field.LIVE_ID)));

				list.add(bookLive);
			}
		 } finally{
			 LetvTools.closeCursor(cursor);
		 }
		
		return list ;
	}
	
	/**
	 * 清除全部预定
	 * */
	public void clearAll(){
		context.getContentResolver().delete(LetvContentProvider.URI_LIVEBOOKTRACE, null, null);
	}
	
	/**
	 * 删除一条记录
	 * */
	public void remove(String programName , String channelName , String code , String play_time){
		String md5_id = MD5.toMd5(programName + code + play_time);
		context.getContentResolver().delete(LetvContentProvider.URI_LIVEBOOKTRACE, DatabaseConstant.LiveBookTrace.Field.MD5_ID+"=?" , new String[] {md5_id+""});
	}
	
	/**
	 * 删除一条记录
	 * */
	public void remove(String md5_id){
		context.getContentResolver().delete(LetvContentProvider.URI_LIVEBOOKTRACE, DatabaseConstant.LiveBookTrace.Field.MD5_ID+"=?" , new String[] {md5_id+""});
	}
	
	/**
	 * 更新一条
	 * */
	public void update(String programName , String channelName , String code , String play_time, boolean isNotify){
		int in = isNotify ? 1 : 0 ;
		String md5_id = MD5.toMd5(programName + code + play_time);
		
		ContentValues cv = new ContentValues();
		cv.put(DatabaseConstant.LiveBookTrace.Field.IS_NOTIFY, in);
		
		context.getContentResolver().update(
				LetvContentProvider.URI_LIVEBOOKTRACE, cv,
				DatabaseConstant.LiveBookTrace.Field.MD5_ID + "=?",
				new String[] { md5_id + "" });
	}
	
	/**
	 * 判断是否记录已存在
	 * */
	public boolean hasLiveBookTrace(String md5_id){
		Cursor cursor = null ;
		try{
			cursor = context.getContentResolver().query(LetvContentProvider.URI_LIVEBOOKTRACE, null, DatabaseConstant.LiveBookTrace.Field.MD5_ID+"=?" , new String[] {md5_id+""}, null);
			int count = cursor.getCount();
			return count > 0;
		}finally{
			LetvTools.closeCursor(cursor);
		}
	}
}
